using System.Collections;
using UnityEngine;
using Utils;

namespace PKGames
{
	public class SpinnerController : MonoBehaviour
	{
		[SerializeField]
		private CanvasGroup _spinnerCanvasGroup;

		[SerializeField]
		private GameObject _spinner;

		private const float SpinSpeed = -360f;

		private const float FadeTime = 0.15f;

		private IEnumerator _fadeCoroutine;

		private void OnEnable()
		{
			Spin(_spinner);
		}

		private void Spin(GameObject spinner)
		{
			StartCoroutine(SpinAsync(spinner));
		}

		private IEnumerator SpinAsync(GameObject spinner)
		{
			while (base.gameObject.activeInHierarchy)
			{
				spinner.transform.Rotate(0f, 0f, -360f * Time.deltaTime);
				yield return null;
			}
		}

		public void ShowSpinner()
		{
			_spinnerCanvasGroup.blocksRaycasts = true;
			if (_fadeCoroutine != null)
			{
				StopCoroutine(_fadeCoroutine);
			}
			_fadeCoroutine = _spinnerCanvasGroup.Fade(0f, 1f, 0.15f);
			StartCoroutine(_fadeCoroutine);
		}

		public void HideSpinner()
		{
			if (_fadeCoroutine != null)
			{
				StopCoroutine(_fadeCoroutine);
			}
			float alpha = _spinnerCanvasGroup.alpha;
			_fadeCoroutine = _spinnerCanvasGroup.Fade(alpha, 0f, 0.15f * alpha, delegate
			{
				_spinnerCanvasGroup.blocksRaycasts = false;
			});
			StartCoroutine(_fadeCoroutine);
		}
	}
}
